package it.nard.controller;

import it.nard.conf.JasperReportManager;
import it.nard.listener.ControlDocumentListener;
import it.nard.listener.TableDocumentListener;
import it.nard.sourceframes.MainFrame;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JPanel;

public abstract  class BasicController  extends AbstractAction{

	
	protected int selectedRow = -1;
	protected JPanel controlPnl = null;
	protected List<? extends Object> objList = null;
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1746548134148723976L;

	public static SimpleDateFormat SDF_4_DB = new SimpleDateFormat("yyyyMMdd");
	
	public static ControlDocumentListener controlDocumentListener = new ControlDocumentListener();
	
	public static TableDocumentListener tableDocumentListener = new TableDocumentListener();
	
	public static JasperReportManager jasperReportManager = new JasperReportManager();
	
//	public static String tempFolder = "D:/tmp/";
	
	
	public static MainFrame mainFrame;
	
	
	
	public abstract String getControllerCode();
	
	public abstract void clearPnl();
	
	public abstract JPanel getMainPanel();
	
	
	public abstract void select (MouseEvent mevent);
	public abstract void onDateSelect();
	
	
	
	public abstract void delete ();
	
	public abstract void insert ();

	private Properties properties;
	
	public BasicController(String name, Icon icon){
		super(name, icon);
		properties = new Properties();
		try {
			properties.load(this.getClass().getResourceAsStream("/properties.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public List<? extends Object> getList4Print() {
		return this.objList;
	}
	public void print (boolean isPrint){
		
		
		
		String tempFile = properties.getProperty("tmpFolder")+getControllerCode();
		
		if(!isPrint){
			tempFile = tempFile.concat(".html");
		}
		else{
			tempFile = tempFile.concat(".pdf");
		}
		
		jasperReportManager.generateReport(this.getList4Print(),getControllerCode(),tempFile , isPrint);
		 
		 Desktop desktop  = Desktop.getDesktop();
		 try {
			desktop.browse(new URI("file://"+tempFile));
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void annulla(){
		this.clearPnl();
	}

	public void actionPerformed(ActionEvent arg0) {

		mainFrame.getContent().removeAll();
		JPanel pnl = getMainPanel();
		if(pnl!=null)
			mainFrame.getContent().add(pnl);
		mainFrame.revalidate();
		mainFrame.repaint();
	}
}
